package com.szrt.department.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.szrt.department.entity.DepartmentEntity;
import com.szrt.department.service.DepartmentService;
import com.szrt.department.service.impl.DepartmentServiceImpl;

public class DepartmentController extends HttpServlet {

	private static final long serialVersionUID = -2248383061552356323L;
	
	
	//套路：把dao申明成此类1个属性   (service也是如此）
	private DepartmentService service = new DepartmentServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//1 设置乱码
		request.setCharacterEncoding("utf-8");
		
		//2 根据method的不同来跳不同的页面
		String method= request.getParameter("method");
		
		if("add".equals(method)){
			add(request, response);
		}else if("delete".equals(method)){
			delete(request, response);
		}else if("findAll".equals(method)){
			findAll(request, response);
		}
	}
	
	
	/**
	 * 查询所有的数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		//1从数据库中查到想要的结果
		List<DepartmentEntity>  departmentList = service.findAll();
		
		Gson gson = new Gson();
		System.err.println(gson.toJson(departmentList));
		
		//2把这个结果放到request里面去  让index.jsp页面显示
		request.setAttribute("departmentList", departmentList);
		
		//3跳页面
		String url ="/department/index.jsp";
		request.getRequestDispatcher(url).forward(request, response);
	}
	

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//2 拿到页面传过来的值
		String departmentCode = request.getParameter("departmentCode");
		
		/**
		 * 1 有异常      counter = -1
		 * 2 没有重复   counter = 0
		 * 3 重复         counter >0
		 */
		//4把这个数据放到数据库中...
		int counter = service.delete(Integer.parseInt(departmentCode));
		
		String url ="";
		if(counter ==1){
			//说明成功
			url ="/department/deleteSuccess.jsp";
		}else{
			url ="/department/deleteFailure.jsp";
		}
		
		
		
		request.getRequestDispatcher(url).forward(request, response);
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//2 拿到页面传过来的值
		//String departmentCode = request.getParameter("departmentCode");
		String departmentName = request.getParameter("departmentName");
		
		//3把这个东西放到作用域requestScope
		DepartmentEntity entity = new DepartmentEntity();
		//entity.setDepartmentCode(Integer.parseInt(departmentCode));
		entity.setDepartmentName(departmentName);
		
		
		//把数据放到请求作用域中
		//request.setAttribute("departmentName", departmentName);
		
		//把数据放到sesssion作用域中
		request.getSession().setAttribute("departmentName", departmentName);
		
		/**
		 * 1 有异常      counter = -1
		 * 2 没有重复   counter = 0
		 * 3 重复         counter >0
		 */
		//4把这个数据放到数据库中...
		int counter = service.add(entity);
		
		String url ="";
		if(counter ==1){
			//说明成功
			url ="/department/success.jsp";
		}else{
			url ="/department/fail.jsp";
		}
		
		
		//5给出提示  请求转发
		//request.getRequestDispatcher(url).forward(request, response);
		
		//5 重定向  302 地址栏改变
		response.sendRedirect(request.getContextPath() +url);
	}

}
